﻿using System.Collections.Generic;
using System.Text;

namespace UnknowWorld.CardGame
{
    /// <summary>
    /// 卡牌数据
    /// </summary>
    [System.Serializable]
    public class Card
    {
        /// <summary>
        /// 编号
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 简介
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 图片地址
        /// </summary>
        public string PicAddress { get; set; }
        /// <summary>
        /// 类型
        /// </summary>
        public CardType Type { get; set; }
        /// <summary>
        /// 能量消耗
        /// </summary>
        public Energy[] Energys { get; set; }
        /// <summary>
        /// 卡牌附加效果
        /// </summary>
        public Effect[] Effects { get; set; }
        /// <summary>
        /// 等级
        /// </summary>
        public int Level { get; set; }
        /// <summary>
        /// 最大等级
        /// </summary>
        public int MaxLevel { get; set; }
        /// <summary>
        /// 是否锁定中
        /// </summary>
        public bool Locked { get; set; }

        /// <summary>
        /// 克隆Card
        /// </summary>
        /// <returns></returns>
        public Card Clone()
        {
            Card cloneCard = new Card() 
            {
                Key = this.Key,
                Name = this.Name,
                Description = this.Description,
                PicAddress = this.PicAddress,
                Type = this.Type,
                Energys = this.Energys,
                Effects = this.Effects,
                Level = this.Level,
                Locked = this.Locked,
                MaxLevel = this.MaxLevel,
            };
            return cloneCard;
        }


        /// <summary>
        /// 特效值
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public int EffectValue(Effect effect)
        {
            int value = 0;
            int index = Level - 1;
            if (index >= effect.Values.Length)
            {
                value = effect.Values[effect.Values.Length - 1];
            }
            if (index < 0)
            {
                return 0;
            }
            return effect.Values[index];
        }

        /// <summary>
        /// 格式化卡牌
        /// </summary>
        /// <returns></returns>
        public string FormatName()
        {
            string name = Name;
            if (Level > 0)
            {
                name = string.Format("{0} +{1}", Name, Level);
            }
            return name;
        }

        /// <summary>
        /// 描述格式化
        /// </summary>
        /// <returns></returns>
        public string FormatDescription()
        {
            StringBuilder desc = new StringBuilder(Description);
            //能量
            if (null != Energys)
            {
                foreach (var energy in Energys)
                {
                    desc.Append(energy.FormatDescription(Level));
                }
            }
            ///特效
            foreach (var effect in Effects)
            {
                var cardEffect = EffectPool.Instance.EffectByKey(effect.Key);
                if (null != cardEffect) desc.Append(cardEffect.FormatDescription(EffectValue(effect)));
            }
            return desc.ToString();
        }

        /// <summary>
        /// 卡牌类型名称
        /// </summary>
        /// <returns></returns>
        public string CardTypeString()
        {
            string typeName = "";
            switch (Type)
            {
                case CardType.Attack:
                    typeName = "攻击卡";
                    break;
                case CardType.Behavior:
                    typeName = "行动卡";
                    break;
                case CardType.Magic:
                    typeName = "魔法卡";
                    break;
                case CardType.Equip:
                    typeName = "装备卡";
                    break;
            }
            return typeName;
        }

        /// <summary>
        /// 卡牌效果（读取用）
        /// </summary>
        [System.Serializable]
        public class Effect
        {
            public string Key { get; set; }
            public int[] Values { get; set; }
        }
    }

    /// <summary>
    /// 卡牌类型
    /// </summary>
    [System.Serializable]
    public enum CardType
    {
        /// <summary>
        /// 攻击卡
        /// </summary>
        Attack,
        /// <summary>
        /// 行动卡
        /// </summary>
        Behavior,
        /// <summary>
        /// 魔法卡
        /// </summary>
        Magic,
        /// <summary>
        /// 装备卡
        /// </summary>
        Equip
    }

}